package com.example.blog.controller;

import com.example.blog.response.ResponseModelDto;
import com.example.blog.response.ResponseModels;
import com.tencentyun.TLSSigAPIv2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "生成腾讯直播的签名",tags = "生成腾讯直播的签名")
@RequestMapping("/tx/")
public class GenUserSig {

    @GetMapping("genUserSig")
    @ApiOperation(value = "genUserSig",notes = "genUserSig")
    public ResponseModelDto<String> genUserSig(String roomNo){
        TLSSigAPIv2 api = new TLSSigAPIv2(1400622978, "e9b2fa5f2e50eb0e47e48e0b72aa65bf14f2a2de362b14f94272085671a8a4d6");
        System.out.println(roomNo);
        String Sig = api.genUserSig(roomNo, 180*86400);
        System.out.print(Sig);
        return ResponseModels.ok(Sig);
    }

    @PostMapping("receive")
    public ResponseEntity<Map<String, Integer>> receive(@RequestBody Map<String, Object> mediaMap) {
        System.out.println("接收到回调。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
        System.out.println(mediaMap.toString());
        // 应答：HTTP STATUS CODE = 200，服务端忽略应答包具体内容，为了协议友好，建议应答内容携带 JSON： {"code":0} 通知可靠性
        Map<String, Integer> responseMap = new HashMap<>(16);
        responseMap.put("code", 0);

        String eventType = mediaMap.get("event_type").toString();
        // 如果非录制文件消息就不处理
//        if (!"NewFileUpload".equals(eventType)) {
//            return new ResponseEntity<Map<String, Integer>>(responseMap, HttpStatus.OK);
//        }
//        String timetableId = mediaMap.get("stream_id").toString();
//        String fileId = mediaMap.get("file_id").toString();
//        if (timetableId.lastIndexOf(IGNORE_MEDIA_TYPE) != -1) {
////            this.deleteMedia(fileId);
//            return new ResponseEntity<>(responseMap, HttpStatus.OK);
//        }
//
//
//        String videoUrl = mediaMap.get("video_url").toString();
//        String streamParam = mediaMap.get("stream_param").toString();
//
//        String fileSize = mediaMap.get("file_size").toString();
//        String fileFormat = mediaMap.get("file_format").toString();
//        String startTime = mediaMap.get("start_time").toString();
//        String endTime = mediaMap.get("end_time").toString();
//        String[] paramsArray = streamParam.split("&");
//        Map<String, String> map = new HashMap<>(paramsArray.length);
//        for (String params : paramsArray) {
//            String[] param = params.split("=");
//            map.put(param[0], param[1]);
//        }
//        String roomId = map.get("groupid");
//        String userIdBase64 = map.get("userid");
//
//        Base64.Decoder decoder = Base64.getDecoder();
//        String lecturerId = null;
//        try {
//            lecturerId = new String(decoder.decode(userIdBase64), "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }

        return new ResponseEntity<>(responseMap, HttpStatus.OK);

    }

}
